//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Xml.XPath;
using DotNetNuke.Common.Lists;
using DotNetNuke.Framework;
using DotNetNuke.Services.Installer.Packages;
using DotNetNuke.Services.Localization;
namespace DotNetNuke.Services.Installer.Installers
{
	public class InstallerFactory
	{
		public static ComponentInstallerBase GetInstaller(string installerType)
		{
			ComponentInstallerBase installer = null;
			switch (installerType) {
				case "File":
					installer = new FileInstaller();
					break;
				case "Assembly":
					installer = new AssemblyInstaller();
					break;
				case "ResourceFile":
					installer = new ResourceFileInstaller();
					break;
				case "AuthenticationSystem":
				case "Auth_System":
					installer = new AuthenticationInstaller();
					break;
				case "DashboardControl":
					installer = new DashboardInstaller();
					break;
				case "Script":
					installer = new ScriptInstaller();
					break;
				case "Config":
					installer = new ConfigInstaller();
					break;
				case "Cleanup":
					installer = new CleanupInstaller();
					break;
				case "Skin":
					installer = new SkinInstaller();
					break;
				case "Container":
					installer = new ContainerInstaller();
					break;
				case "Module":
					installer = new ModuleInstaller();
					break;
				case "CoreLanguage":
					installer = new LanguageInstaller(LanguagePackType.Core);
					break;
				case "ExtensionLanguage":
					installer = new LanguageInstaller(LanguagePackType.Extension);
					break;
				case "Provider":
					installer = new ProviderInstaller();
					break;
				case "SkinObject":
					installer = new SkinControlInstaller();
					break;
				case "Widget":
					installer = new WidgetInstaller();
					break;
				default:
					DotNetNuke.Common.Lists.ListController listController = new DotNetNuke.Common.Lists.ListController();
					ListEntryInfo entry = listController.GetListEntryInfo("Installer", installerType);
					if (entry != null && !string.IsNullOrEmpty(entry.Text)) {
						installer = (ComponentInstallerBase)Reflection.CreateObject(entry.Text, "Installer_" + entry.Value);
					}
					break;
			}
			return installer;
		}
		public static ComponentInstallerBase GetInstaller(XPathNavigator manifestNav, PackageInfo package)
		{
			string installerType = Util.ReadAttribute(manifestNav, "type");
			string componentVersion = Util.ReadAttribute(manifestNav, "version");
			ComponentInstallerBase installer = GetInstaller(installerType);
			if (installer != null) {
				installer.Package = package;
				installer.Type = installerType;
				if (!string.IsNullOrEmpty(componentVersion)) {
					installer.Version = new Version(componentVersion);
				} else {
					installer.Version = package.Version;
				}
				if (package.InstallerInfo.InstallMode != InstallMode.ManifestOnly || installer.SupportsManifestOnlyInstall) {
					installer.ReadManifest(manifestNav);
				}
			}
			return installer;
		}
	}
}
